package com.yricky.poetcard.harmony.slice;


import com.yricky.jinrishici.PoetModel;
import com.yricky.poetcard.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;
import ohos.distributedschedule.interwork.DeviceInfo;
import ohos.distributedschedule.interwork.DeviceManager;

import java.io.File;
import java.util.List;

public class UIAbilitySlice extends AbilitySlice {
    private static final String BUNDLE_NAME ="com.yricky.poetcard";
    private static final String ABILITY_NAME="com.yricky.poetcard.harmony.UIAbility";
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_ui);
        Text text=(Text) findComponentById(ResourceTable.Id_content_text);
        PoetModel poetModel=PoetModel.getModel(new File(getExternalFilesDir(null),"Poet"));
        Button button=(Button)findComponentById(ResourceTable.Id_btn_update);
        button.setClickedListener((c)->{
            poetModel.update((status -> {
                if(status.equals("success"))
                    text.setText(poetModel.getJSONString());
                else
                    text.setText("Failed:"+status);
            }));
        });
        findComponentById(ResourceTable.Id_btn_flow).setClickedListener(mStartRemoteFAListener);
    }

    @Override
    protected void onActive() {
        super.onActive();
        Text text=(Text) findComponentById(ResourceTable.Id_content_text);
        PoetModel poetModel=PoetModel.getModel(new File(getExternalFilesDir(null),"Poet"));
        if(poetModel.getJSONString()!=null) {
            text.setText(poetModel.getJSONString());
        }
    }



    private final Component.ClickedListener mStartRemoteFAListener = arg0 -> {
        /*
        List<DeviceInfo> onlineDevices = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE);
        for (DeviceInfo deviceInfo : onlineDevices) {
                String deviceId=deviceInfo.getDeviceId();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId(deviceId)
                        .withBundleName(BUNDLE_NAME)
                        .withAbilityName(ABILITY_NAME)
                        .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE)
                        .withAction("A")
                        .build();
                Intent intent = new Intent();
                intent.setOperation(operation);
                // 通过AbilitySlice包含的startAbility接口实现跨设备启动FA
                startAbility(intent);
                ToastDialog toastDialog=new ToastDialog(this);
                toastDialog.setText(deviceInfo.getDeviceName());
                toastDialog.show();

        }*/

    };



}
